'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  //登录
  async login() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.query(`select * from login where username = "${username}" and password = "${password}"`)
    console.log(ctx.request.body);



    //遍历登录成功的数据存入到datas
    const datas = data.map(item => {
      return {
        url: item.url,
        name: item.name,
      }
    })

    if (data.length) {
      //登陆成功传入datas
      ctx.body = {
        code: 1,
        datas,
        token: jwt.sign({ ...data }, '1901A', { expiresIn: '4m' }),
      }
    } else {
      ctx.body = {
        code: 0,
      }
    }
  }

  //注册
  async register() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('login', { username })

    if (!data && username !== '' && password !== '') {
      app.mysql.insert('login', { username, password })

      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 0,
        data
      }
    }
  }

  //渲染新房数据
  async newHouse() {
    const { app, ctx } = this
    const data = await app.mysql.select('newHouse')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //获取新房详情数据
  async Detail() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('newHouse', { id })

    ctx.body = {
      code: 1,
      data
    }
  }

  //新房数据筛选
  async newScreening() {
    const { app, ctx } = this
    const { value2, value3, value4 } = ctx.request.body
    let data = await app.mysql.select("newhouse")
    console.log(value2);
    

    //判定价格
    if (value2) {
      data = data.filter(v => {
        if (value2 === '<10') {
          return v.sum < 10
        } else if (value2 === "20-30") {
          return v.sum >= 20 && v.sum < 30
        } else if (value2 === "30-50") {
          return v.sum >= 30 && v.sum < 50
        } else if (value2 === "50-100") {
          return v.sum >= 50 && v.sum < 100
        } else if (value2 === ">100") {
          return v.sum > 100
        } else {
          return v
        }
      })
    }
    //判定类型
    if (value3) {
      data = data.filter(v => v.type.includes(value3)) 
    }
    // if(value4){
    //   data = data.filter(v => v.direction.includes(value4))
    // }

    ctx.body = {
      code: 1,
      data
    }
  }

  //渲染消息数据
  async messageList() {
    const { app, ctx } = this
    const data = await app.mysql.select('message')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //消息数据删除
  async messageDel() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('message',{ id })

    ctx.body = {
      code: 1,
      data
    }
  }

  //渲染团购列表
  async abulkList() {
    const { app, ctx } = this
    const data = await app.mysql.select('group')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //团购详情数据
  async abulkDetail() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('group', { id })

    ctx.body = {
      code: 1,
      data
    }
  }

  //求租数据渲染
  async solicitingList() {
    const { app, ctx } = this
    const data = await app.mysql.select('renthouse')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //求购数据渲染
  async askBuyList() {
    const { app, ctx } = this
    const data = await app.mysql.select('buyhouse')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

   //排行榜数据渲染
   async rankingList() {
    const { app, ctx } = this
    const data = await app.mysql.select('newhouse')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //直播数据渲染
  async liveList() {
    const { app, ctx } = this
    const data = await app.mysql.select('live')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //楼市圈数据渲染
  async circleList() {
    const { app, ctx } = this
    const data = await app.mysql.select('circle')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }


}

module.exports = HomeController;
